﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;

public partial class NewTemplates_LatestIssue_Default : Acms.AcmsControls.TemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Post> children = AcmsContext.CurrentPost.GetChildren(AcmsContext.SiteVersion);
            if (children != null)
            {
                foreach (Post child in children)
                {
                    if (child.TemplateName == "Issue Template")
                    {
                        latestIssue = child;
                        break;
                    }
                }
            }
        }
    }

    private Post latestIssue;

    protected void topics_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
    {
        Topic topic = e.Item as Topic;
        if (latestIssue != null && topic != null)
        {
            if (e.ItemType == AjaxControlToolkit.AccordionItemType.Header)
            {
                HyperLink lnkTopic = e.AccordionItem.FindControl("lnkTopic") as HyperLink;
                if (lnkTopic != null)
                {
                    lnkTopic.Text = topic.Name;
                    lnkTopic.NavigateUrl = string.Concat(latestIssue.Url, latestIssue.Url.Contains("?") ? "&" : "?", "SelctedTopic=", topic.Id);
                }
            }
            else if (e.ItemType == AjaxControlToolkit.AccordionItemType.Content)
            {
                SubTemplateLoader ucSelectedItemTemplate = e.AccordionItem.FindControl("ucSelectedItemTemplate") as SubTemplateLoader;
                if (ucSelectedItemTemplate != null)
                {
                    int totalRecords = 0;
                    List<Post> posts = Post.GetAllByParentGuidAndTopicId(latestIssue.PostGuid, topic.Id, AcmsContext.SiteVersion, false, 0, 1, ref totalRecords);
                    if (posts.Count == 1)
                    {
                        ucSelectedItemTemplate.CurrentPost = posts[0];
                    }
                }
            }
        }
    }

    protected void dsTopics_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Topic> e)
    {
        e.SetData(Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.SiteId, "ReserveSociety"));
    }

}
